package com.yanqu.road.dao.impl.zhaoshang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.zhaoshang.ZsProjectReward;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossZsProjectRewardDaoImpl extends TempDataObjectDao<ZsProjectReward> {


    public Map<Long, List<ZsProjectReward>> getZsWarReportMap(int endTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<ZsProjectReward>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_c_zs_project_reward where  dbid > (select max(dbid) from t_c_zs_project_reward where addTime > ? and addTime < ? ) and sendReward = 0";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            //加快速度，但是可能间隔3天没有数据，那就从0开始取了
            call.setLong(1, endTime - 3600 * 72);
            call.setLong(2, endTime);
            rs = call.executeQuery();
            while (rs.next()) {
                ZsProjectReward data = parse(rs);
                if(!dataMap.containsKey(data.getUserId())){
                    dataMap.put(data.getUserId(), new ArrayList<>());
                }
                dataMap.get(data.getUserId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(ZsProjectReward data) {
        PreparedStatement call = null;
        String sql = "insert into t_c_zs_project_reward(projectId, seatId, sitTime, type, reward, duration, addTime, sendReward, templateId, enemyUserId, enemyBaseInfo," +
                "userId,serverId,userBaseInfo) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getProjectId());
            call.setInt(i++, data.getSeatId());
            call.setLong(i++, data.getSitTime());
            call.setInt(i++, data.getType());
            call.setString(i++, data.getReward());
            call.setInt(i++, data.getDuration());
            call.setInt(i++, data.getAddTime());
            call.setBoolean(i++, data.isSendReward());
            call.setInt(i++, data.getTemplateId());
            call.setLong(i++, data.getEnemyUserId());
            call.setString(i++, data.getEnemyBaseInfoJsonStr());
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getServerId());
            call.setString(i++, data.getUserBaseInfoJsonStr());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(ZsProjectReward data) {
        PreparedStatement call = null;
        String sql = "update t_c_zs_project_reward set sendReward=?,type=?, reward=?, duration=?, addTime=?,templateId=?, enemyUserId=?, enemyBaseInfo=?, userBaseInfo=?, serverId=? " +
                "where userId=? and sitTime=? and projectId=? and seatId=?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setBoolean(i++, data.isSendReward());
            call.setInt(i++, data.getType());
            call.setString(i++, data.getReward());
            call.setInt(i++, data.getDuration());
            call.setInt(i++, data.getAddTime());
            call.setInt(i++, data.getTemplateId());
            call.setLong(i++, data.getEnemyUserId());
            call.setString(i++, data.getEnemyBaseInfoJsonStr());
            call.setString(i++, data.getUserBaseInfoJsonStr());
            call.setLong(i++, data.getServerId());
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getSitTime());
            call.setLong(i++, data.getProjectId());
            call.setInt(i++, data.getSeatId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected ZsProjectReward parseData(ResultSet rs) throws SQLException {
        ZsProjectReward data = new ZsProjectReward();
        data.setSendReward(rs.getBoolean("sendReward"));
        data.setUserId(rs.getLong("userId"));
        data.setDuration(rs.getInt("duration"));
        data.setReward(rs.getString("reward"));
        data.setType(rs.getInt("type"));
        data.setTemplateId(rs.getInt("templateId"));
        data.setSeatId(rs.getInt("seatId"));
        data.setProjectId(rs.getLong("projectId"));
        String baseUserInfoStr = rs.getString("enemyBaseInfo");
        if(!StringUtils.isNullOrEmpty(baseUserInfoStr)){
            UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
            if(null != userBaseInfo) {
                data.setEnemyBaseInfo(userBaseInfo);
            }
        }
        baseUserInfoStr = rs.getString("userBaseInfo");
        if(!StringUtils.isNullOrEmpty(baseUserInfoStr)){
            UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
            if(null != userBaseInfo) {
                data.setUserBaseInfo(userBaseInfo);
            }
        }
        data.setEnemyUserId(rs.getLong("enemyUserId"));
        data.setSitTime(rs.getLong("sitTime"));
        data.setAddTime(rs.getInt("addTime"));
        data.setServerId(rs.getLong("serverId"));
        return data;
    }

}
